package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabRptCampusArea;
import com.ysd.lis.entity.lab.LabRptGroup;
import com.ysd.lis.mapper.lab.LabRptCampusAreaMapper;
import com.ysd.lis.mapper.lab.LabRptGroupMapper;
import com.ysd.lis.request.lab.RptCampusDto;
import com.ysd.lis.request.lab.RptCampusParam;
import com.ysd.lis.service.lab.LabRptCampusAreaService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author prs
 * @since 2025-02-21
 */
@Service
public class LabRptCampusAreaServiceImpl extends ServiceImpl<LabRptCampusAreaMapper, LabRptCampusArea> implements LabRptCampusAreaService {

    @Autowired
    LabRptCampusAreaMapper labRptCampusAreaMapper;
    @Override
    public Result findRptCampusAreaList(RptCampusParam param) {
        MPJLambdaWrapper<LabRptCampusArea> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LabRptCampusArea.class);
        queryWrapper.selectAs("rpt", LabRptGroup::getRptGroupName, LabRptCampusArea::getRptGroupName);
        queryWrapper.leftJoin(LabRptGroup.class, "rpt", p -> p.eq(LabRptGroup::getRptGroup, LabRptCampusArea::getRptGroup).eq(LabRptGroup::getDelFlag, 0));
        queryWrapper.eq(LabRptCampusArea::getCampusCode, param.getCampusCode())
                .eq(LabRptCampusArea::getDelFlag, 0);
        List<LabRptCampusArea> detailList = labRptCampusAreaMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", detailList);
    }

    @Override
    public Result batchDeleteRptCampusArea(RptCampusDto dto) {
        List<LabRptCampusArea> labRptCampusAreaList = dto.getLabRptCampusAreaList();
        List<String> ids = labRptCampusAreaList.stream().map(LabRptCampusArea::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result deleteRptCampusArea(LabRptCampusArea campusArea) {
        int deleteById = labRptCampusAreaMapper.deleteById(campusArea.getId());
        if (deleteById > 0) {
            return Result.succ(1, "删除成功", "");
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Autowired
    LabRptGroupMapper labRptGroupMapper;
    @Override
    public Result getRptGroupsByCampus(RptCampusParam param) {
        MPJLambdaWrapper<LabRptGroup> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LabRptGroup.class);
         queryWrapper.eq(LabRptGroup::getDelFlag, 0);
         if(ToolsUtils.isNotEmpty(param.getSearchValue())){
             queryWrapper.and(wrapper -> wrapper.like(LabRptGroup::getRptGroupName, param.getSearchValue()).or().like(LabRptGroup::getRptGroup, param.getSearchValue()));
         }
        StringBuffer existsSql = new StringBuffer("SELECT 1 FROM LAB_RPT_CAMPUS_AREA t2 WHERE t2.DEL_FLAG=0 and t.RPT_GROUP = t2.RPT_GROUP");
        queryWrapper.notExists(existsSql.toString());
        List<LabRptGroup> detailList = labRptGroupMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", detailList);
    }

    @Override
    public Result addRptCampusArea(LabRptCampusArea rptCampusArea) {
        int insert = labRptCampusAreaMapper.insert(rptCampusArea);
        if (insert > 0) {
            return Result.succ(1, "添加成功", rptCampusArea);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }
}
